package com.weishenai.customerassistant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishenai.customerassistant.entity.FactFinder;
import com.weishenai.customerassistant.mapper.FactFinderMapper;
import com.weishenai.customerassistant.service.FactFinderService;
import com.weishenai.customerassistant.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FactFinderServiceImpl extends ServiceImpl<FactFinderMapper, FactFinder> implements FactFinderService {

    @Autowired
    private FactFinderMapper factFinderMapper;

    @Override
    public FactFinder login(String weixinId) {
        QueryWrapper<FactFinder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("weixin_id", weixinId);
        return factFinderMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer createFactFinder(FactFinder factFinder) {
        factFinder.setCreatedTime(DateUtils.getTime());
        return factFinderMapper.insert(factFinder);
    }

    @Override
    public Integer delFactFinder(String weixinId) {
        QueryWrapper<FactFinder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("weixin_id", weixinId);
        return factFinderMapper.delete(queryWrapper);

    }

    @Override
    public Integer updateFactFinder(FactFinder factFinder) {
        return factFinderMapper.updateById(factFinder);
    }

    @Override
    public IPage<FactFinder> selectByFactName(String substation, Integer pageNumber, Integer pageSize, String name) {
        QueryWrapper<FactFinder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("substation_name", substation);
        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.like("fact_name", name);
        }
        queryWrapper.orderByDesc("created_time");
        IPage<FactFinder> page = new Page<>(pageNumber, pageSize);
        return factFinderMapper.selectPage(page, queryWrapper);
    }

    @Override
    public IPage<FactFinder> selectAll(String substation, Integer pageNumber, Integer pageSize) {
        QueryWrapper<FactFinder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("substation_name", substation);
        queryWrapper.orderByDesc("created_time");
        IPage<FactFinder> page = new Page<>(pageNumber, pageSize);
        return factFinderMapper.selectPage(page, queryWrapper);
    }

    @Override
    public FactFinder selectById(Integer id) {
        QueryWrapper<FactFinder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return factFinderMapper.selectOne(queryWrapper);
    }

    @Override
    public List<FactFinder> listFactFinder(String substation) {
        return factFinderMapper.selectList(
                Wrappers.lambdaQuery(FactFinder.class)
                        .eq(FactFinder::getSubstationName, substation));
    }

    @Override
    public Long getCountBySubstation(String substation) {
        return factFinderMapper.selectCount(
                Wrappers.lambdaQuery(FactFinder.class)
                        .eq(FactFinder::getSubstationName,substation));
    }
}
